
default_platform(:ios)

platform :ios do
  desc "Perform tests"
  lane :test do
    test_macOS
    test_iOS  
    test_tvOS
  end

  lane :test_macOS do |options|
    scan( 
      package_path: "./",
      scheme: "APNGKit",
      clean: true, 
      destination: "platform=macOS",
      result_bundle: true
    )
  end

  lane :test_iOS do |options|
    scan(
      package_path: "./",
      scheme: "APNGKit",
      clean: true, 
      destination: "platform=iOS Simulator,name=iPhone 14",
      result_bundle: true
    )
  end

  lane :test_tvOS do |options|
    scan(
      package_path: "./",
      scheme: "APNGKit",
      clean: true, 
      destination: "platform=tvOS Simulator,name=Apple TV",
      result_bundle: true
    )
  end
  
  lane :release do |options|
    target_version = options[:version]
    raise "The version is missed. Use `fastlane release version:{version_number}`.`" if target_version.nil?
    
    ensure_git_branch
    ensure_git_status_clean
    
    skip_tests =  options[:skip_tests]
    test unless skip_tests
    
    lint

    increment_version_number(
        version_number: target_version,
        xcodeproj: "Demo/Demo.xcodeproj"
    )
    version_bump_podspec(path: "APNGKit.podspec", version_number: target_version)
    
    release_content = read_changelog(excluded_markdown_elements: [])
    change_log(version: target_version)

    Action.sh "git add -A"
    Actions.sh "git commit -am \"Bump version to #{target_version}\""
    
    Actions.sh("git tag -s #{target_version} -m ''")
    
    push_to_git_remote
    
    set_github_release(
      repository_name: "onevcat/APNGKit",
      api_token: ENV['GITHUB_TOKEN'],
      name: target_version,
      tag_name: target_version,
      description: release_content
    )
    
    pod_push
  end

  lane :change_log do |options|
    target_version = options[:version]
    raise "The version is missed. You need to specify a version parameter." if target_version.nil?
    stamp_changelog(section_identifier: target_version, git_tag: target_version, stamp_datetime_format: "%F")
  end

  desc "Lint"
  lane :lint do
    pod_lib_lint
    spm
  end
end
